package com.reptile;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/4/16 21:18
 * @since JDK 1.8
 * 正则表达式
 */
public class ReptileUtils {

    public static void main(String[] args) {
       String str="";
        matcherContents("","src=(.*?)");
    }

    public static void test(){
        String content=getWebContent("https://s.weibo.com/top/summary?cate=realtimehot");
        String s=matcherContents(content,"/<td .*?>(.*?)<td>/");
        System.out.println(s);
    }

    /**
     *
     * @param url1 网址
     * @return //获取网址里面的内容
     */
    public static String getWebContent(String url1){
        URL url;
        int responsecode;
        HttpURLConnection urlConnection;
        BufferedReader reader;
        String line;
        StringBuffer stringBuffer=new StringBuffer();
        try{
            url=new URL(url1);
            //打开URL
            urlConnection = (HttpURLConnection)url.openConnection();
            //获取服务器响应代码
            responsecode=urlConnection.getResponseCode();
            if(responsecode==200){
                //得到输入流，即获得了网页的内容
                reader=new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                while((line=reader.readLine())!=null){
                    System.out.println(line);
                    stringBuffer.append(line);
                }

            }
            else{
                System.out.println("获取不到网页的源码，服务器响应代码为："+responsecode);
            }
        }
        catch(Exception e){
            System.out.println("获取不到网页的源码,出现异常："+e);
        }
        return stringBuffer.toString();
    }


    public static String matcherContents(String content,String regx){
        Pattern p = Pattern.compile(regx);
        //让正则对象和要操作的字符串相关联；
        Matcher m = p.matcher(content);
        //m.find():将规则作用到字符串上，并进行符合规则的字串查找；
        StringBuilder str=new StringBuilder();
        while (m.find()) {
            str.append(m.group());
            // System.out.println(m.start() + "---" + m.end()); //获取找到字串的始末索引(包含头，不包含尾);
        }
        return str.toString();
    }


    public static List<String> matcherContent(String content,String reg){
        Pattern p = Pattern.compile(reg);
        //让正则对象和要操作的字符串相关联；
        Matcher m = p.matcher(content);
        //m.find():将规则作用到字符串上，并进行符合规则的字串查找；
        List<String> stringList=new ArrayList<>();
        while (m.find()) {
            stringList.add(m.group());
           // System.out.println(m.start() + "---" + m.end()); //获取找到字串的始末索引(包含头，不包含尾);
        }
        return stringList;
    }


    /**
     *
     * @param pre 开始部分
     * @param suf 结束部分
     * @param content 整段文章
     * @return 截取部分
     */
    public static String getContentBySlice(String pre,String suf,String content){
        String regex1 = pre+".*?"+suf;
        try {
            Pattern p = Pattern.compile(regex1);
            Matcher m = p.matcher(content);
            StringBuffer stringBuffer=new StringBuffer();
            while(m.find()){
                stringBuffer.append(m.group()+"\n");
            }
            return stringBuffer.toString();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     *
     * @param content 网页标签内容
     * @param regex  网页特定标签部分
     * @return
     */
    public static String getWebAttrByName(String content,String regex){
        String str=" ";
        String regex1 = regex+".*?"+str;
        try {
            Pattern p = Pattern.compile(regex1);
            Matcher m = p.matcher(content);
            StringBuffer stringBuffer=new StringBuffer();
            while(m.find()){
                stringBuffer.append(m.group()+"\n");
            }
            return stringBuffer.toString();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


    /**
     *
     * @param content 网页内容
     * @param regex 标签
     * @return 被标签包裹的内容
     */
    public static String getContentByTab(String content,String regex){
        String regex1 = "<"+regex+".*?>"+".*?</"+regex+">";
        try {
            Pattern p = Pattern.compile(regex1);
            Matcher m = p.matcher(content);
            StringBuffer stringBuffer=new StringBuffer();
            while(m.find()){
                stringBuffer.append(m.group()+"\n");
            }
            return stringBuffer.toString();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

}
